const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`

}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * 返回时间字符串
 * @param {Long} timestamp 
 */
const timeSpan = (timestamp) => {

  var dateTime = new Date(timestamp);
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();
  var hour = dateTime.getHours();
  var minute = dateTime.getMinutes();
  var second = dateTime.getSeconds();

  var now = new Date().getTime();

  var milliseconds = 0;
  var timeSpanStr;

  milliseconds = now - timestamp;

  if (milliseconds <= 1000 * 60 * 1) {
    timeSpanStr = '刚刚';
  } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
    timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
  } else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
    timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
  } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
    timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
  } else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == new Date().getFullYear()) {
    timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
  } else {
    // timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    timeSpanStr = year + '-' + month + '-' + day;
  }
  return timeSpanStr;
}

/**
 * 判断是否全面屏
 * 
 */
const isFullScreen = () => {
  let systemInfo = wx.getSystemInfoSync();
  // 如果系统信息返回的状态栏高度大于20,认为是全面屏手机
  if (systemInfo.statusBarHeight > 20) {
    return true;
  }
  return false;
}

/**
 * 计算时间差,默认返回毫秒差值
 * @param {Long} start 
 * @param {Long} end 
 * @param {String} show 获取结果,传参['d':天数差值,'h':小时差值,'m':分差值,'s':秒差值,'ms':毫秒差值]
 */
const timeGap = (start, end, show = "ms") => {
  // 两个时间戳相差的毫秒数
  let usedTime = end - start;

  let array = new Array(),
    result = 0;

  // 计算天数
  let d = Math.floor(usedTime / (24 * 3600 * 1000));

  array.push({
    name: "d",
    value: d
  });

  // 计算出小时数
  let h = Math.floor(usedTime / (60 * 60 * 1000) - d * 24);

  array.push({
    name: "h",
    value: h
  });

  // 计算相差分钟数
  let m = Math.floor((usedTime / (60 * 1000)) - d * 24 * 60 - h * 60);

  array.push({
    name: "m",
    value: m
  });

  // 计算相差秒数
  let s = Math.floor(usedTime / 1000 - d * 24 * 60 * 60 - h * 60 * 60 - m * 60);

  array.push({
    name: "s",
    value: s
  });

  // 计算相差毫秒
  let ms = Math.floor(usedTime - d * 24 * 60 * 60 * 1000 - h * 60 * 60 * 1000 -
    m * 60 * 1000 - s * 1000);

  array.push({
    name: "ms",
    value: ms
  });

  for (let i = 0; i < array.length; i++) {
    if (show == array[i].name) {
      result = array[i].value;
      break;
    }
  }

  return result;
}

/**
 * 计算某叔的百分比
 * @param {Integer} num 
 * @param {Integer} rate 默认50%
 */
const percentage = (num, rate = 50) => {

  if (Number.isFinite(num) && Number.isFinite(rate)) {
    return Math.round(num * (rate / 100));
  }

  return 180;
}

/**
 * 获取缩放后的图片尺寸
 * @param {Object} e 
 */
const imgScaleSize = (e) => {
  let imageSize = {};
  //图片原始宽 
  let originalWidth = e.width == 0 ? 500 : e.width;
  //图片原始高 
  let originalHeight = e.height == 0 ? 500 : e.height;
  //图片高宽比
  let originalScale = originalHeight / originalWidth;
  let systemInfo = wx.getSystemInfoSync();
  let windowWidth = systemInfo.windowWidth;
  let windowHeight = systemInfo.windowHeight;
  //屏幕高宽比
  let windowscale = windowHeight / windowWidth;
  //图片高宽比小于屏幕高宽比 
  if (originalScale < windowscale) {
    //图片缩放后的宽为屏幕宽 
    imageSize.width = windowWidth;
    imageSize.height = (windowWidth * originalHeight) / originalWidth;
  } else { //图片高宽比大于屏幕高宽比 
    //图片缩放后的高为屏幕高 
    imageSize.height = windowHeight;
    imageSize.width = (windowHeight * originalWidth) / originalHeight;
  }
  return imageSize;
}

const placeholderArr = () => {
  let placeholders = ['高颜值的你,一定想说点什么~', '评论点赞,都要勇往直前~', '咦,不留点评论还想走?', '评论一句,前排打call!', '今天我最靓,评论我在行!', '说点大家喜欢的话吧~', '精彩评论,还是得看我~', '说点什么吧~'];
  let index = Math.floor(Math.random() * 7);
  return placeholders[index];
}

/**
 * 随机提示搜索提示
 * @param {Array} arr 
 */
const searchPlaceholder = (arr) => {
  if (!arr || arr.length <= 0) {
    return '日落';
  }
  let index = Math.floor(Math.random() * arr.length);
  return arr[index];
}

module.exports = {
  formatTime: formatTime,
  timeSpan: timeSpan,
  isFullScreen: isFullScreen,
  timeGap: timeGap,
  percentage: percentage,
  imgScaleSize: imgScaleSize,
  placeholderArr: placeholderArr,
  searchPlaceholder: searchPlaceholder
}